package com.yuexunit.finance.system.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.system.entity.CapitalAccountInfoEntity;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.StringUtil;

/**
 * @author wenwenlong
 */
@Implements(BankInfoService.class)
public class BankInfoServiceImpl implements BankInfoService {

	@Override
	@Transactional
	public Long createBankInfo(CapitalAccountInfoEntity entity) {
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.insert(entity);
		return entity.getAccountInfoId();
	}

	@Override
	@Transactional
	public void updateBankInfo(CapitalAccountInfoEntity entity) {
		entity.setUpdateDateTime(DateUtil.now());
		entity.setAccountAmount(entity.getAccountBalance().add(entity.getAccountLoan()));
		PersistUtil.update(entity);
	}

	@Override
	@Transactional
	public void deleteBankInfo(Long accountInfoId) {
		PersistUtil.delete(CapitalAccountInfoEntity.class, accountInfoId);
	}

	@Override
	public PageList<CapitalAccountInfoEntity> inquireBankInfoPageList(String accountBank, String accountBankCard, String accountBankName, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(CapitalAccountInfoEntity.class);
		if (StringUtil.isNotBlank(accountBank)) {
			criteria.add(Restrictions.like("accountBank", "%" + accountBank + "%"));
		}
		if (StringUtil.isNotBlank(accountBankCard)) {
			criteria.add(Restrictions.like("accountBankCard", "%" + accountBankCard + "%"));
		}
		if (StringUtil.isNotBlank(accountBankName)) {
			criteria.add(Restrictions.like("accountBnakName", "%" + accountBankName + "%"));
		}
		criteria.addOrder(Order.desc("top"));
		return PersistUtil.get(criteria, page);
	}
	

	@Override
	public List<CapitalAccountInfoEntity> inquireBankInfoList() {
		DetachedCriteria criteria = DetachedCriteria.forClass(CapitalAccountInfoEntity.class);
		return PersistUtil.get(criteria);
	}

	@Override
	public CapitalAccountInfoEntity inquireBankInfoDetail(Long accountInfoId) {
		return PersistUtil.get(CapitalAccountInfoEntity.class, accountInfoId);
	}
}
